using Vte;

namespace Zoo
{

public class Terminal : Vte.Terminal
{
    private int x_display;
    private int pid;

    public Terminal()
    {
        this.x_display = 4;
        this.pid = -1;
        init_terminal();
    }

    private void init_terminal()
    {
        set_emulation("xterm");
        set_word_chars("-A-Za-z0-9,./?%&#:_");
        set_mouse_autohide(true);
        set_size(80, 16);
        set_scroll_on_keystroke(true);
        set_scroll_on_output(true);
    }

    public void start()
    {
        string[] args = {"bash"};
        string[] envv = {"DISPLAY=:%d".printf(this.x_display)};

        this.pid = fork_command(args[0], args, envv, Path.get_dirname("."),
                                false, false, false);

        if (this.pid == -1) {
            stderr.printf("There was a problem running \"" + args[0] + "\"\n");
            return;
        }

        reset(true, true);
    }

    public void clear_prompt()
    {
        for (int i = 0; i < 100; i++)
            feed_child("\b\b\b\b\b\b\b\b\b\b", -1);
    }

    public void send_command(string command)
    {
        clear_prompt();
        feed_child(command, -1);
        feed_child("\r", -1);
    }

    public void clear()
    {
        feed_child("\f", -1);
    }

    /*
    public void set_display(int display)
    {
        gchar* command = g_strdup_printf("export DISPLAY=:%d", display);
        g_print("--- %s ---\n", command);
        terminal_send_command(terminal, command);
        g_free(command);
    }

    public void change_target(string target)
    {
        gchar* command = g_strdup_printf("sb-conf select %s", target);
        terminal_send_command(terminal, command);
        terminal_clear(terminal);
        g_free(command);
    }
    */
}

} // namespace Zoo

